package com.android.bookgarden.network;


import android.util.Log;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.Response;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import okhttp3.Headers;

/**
 * 请求工具
 * Created by zouqh on 2018/3/26.
 */

public class GetDataImplement implements GetData {
    @Override
    public void sendHttp(Map<String, String> value, String url, OkgoCallback callback, HttpType httpType, int code) {
        if (httpType == HttpType.POST) {
            if (null ==value){
                sendGet( url, callback, code);
            }else{
                sendPost(value, url, callback, code);
            }

        } else {
            if (null ==value){
                sendGet( url, callback, code);
            }else{
                sendGet(value, url, callback, code);
            }

        }
    }

    @Override
    public void sendHttp(String json, String url, final OkgoCallback callback, HttpType httpType, final int code) {
        OkGo.<String>post(url).upJson(json)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        callback.onSuccess(response.body(), code);
//                        callback.onSuccess(dataBean.getData().toString());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        callback.onFail(response,code);

                    }
                });
    }

    @Override
    public void sendHttpGet(String url, OkgoCallback callback, int code) {
        sendGet(url,callback,code);
    }

    @Override
    public void sendHttpHeader(Map<String, String> value, String url, OkgoCallback callback, HttpType httpType, int code, HttpHeaders headers) {

        if (httpType==HttpType.GET){
            if (null==value){
                sendGetHeader(url,callback,code,headers);
            }else{
                    sendGetHeader(value,url,callback,code,headers);
            }
        }else if (httpType==HttpType.POST){
            if (null==value){
                sendPostHeader(url,callback,code,headers);
            }else{

                    sendPostHeader(value,url,callback,code,headers);

            }
        }else if (httpType==HttpType.PUT){
            if (null==value){
                sendPostHeader(url,callback,code,headers);
            }else{

              sendGetHeader(value,url,callback,code,headers);

            }
        }else if (httpType==HttpType.DELETE){
            sendDelete(value,url,callback,code,headers);
        }



    }

    private void sendPost(Map<String, String> value, String url, final OkgoCallback callback, final int code) {

        OkGo.<String>post(url).params(value)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        callback.onSuccess(response.body(), code);
//                        callback.onSuccess(dataBean.getData().toString());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        callback.onFail(response,code);

                    }
                });
    }

    private void sendGet(Map<String, String> value, String url, final OkgoCallback callback, final int code) {

        OkGo.<String>get(url).params(value)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        callback.onSuccess(response.body(), code);
//                        callback.onSuccess(dataBean.getData().toString());

                    }

                    @Override
                    public void onError(Response<String> response) {
                        callback.onFail(response,code);

                    }
                });
    }

    private void sendGet( String url, final OkgoCallback callback, final int code) {
        Log.e("url---",url);
        OkGo.<String>get(url)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        callback.onSuccess(response.body(), code);
//                        callback.onSuccess(dataBean.getData().toString());

                    }

                    @Override
                    public void onError(Response<String> response) {
                        callback.onFail(response,code);

                    }
                });
    }


    private void sendPostHeader(Map<String, String> value, String url, final OkgoCallback callback, final int code,HttpHeaders headers) {

        OkGo.<String>post(url).headers(headers).params(value)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        callback.onSuccess(response.body(), code);
//                        callback.onSuccess(dataBean.getData().toString());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        callback.onFail(response,code);

                    }
                });
    }

    private void sendPostHeader( String url, final OkgoCallback callback, final int code,HttpHeaders headers) {

        OkGo.<String>post(url).headers(headers)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        callback.onSuccess(response.body(), code);
//                        callback.onSuccess(dataBean.getData().toString());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        callback.onFail(response,code);

                    }
                });
    }

    private void sendGetHeader( String url, final OkgoCallback callback, final int code,HttpHeaders headers) {

        OkGo.<String>get(url).headers(headers)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        callback.onSuccess(response.body(), code);
//                        callback.onSuccess(dataBean.getData().toString());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        callback.onFail(response,code);

                    }
                });
    }

    private void sendDelete( Map<String, String> params,String url, final OkgoCallback callback, final int code,HttpHeaders headers) {
        StringBuilder sb = new StringBuilder();
        sb.append(url);
        if (params != null && params.size() != 0) {
            sb.append("?");
            for (Map.Entry<String, String> entry : params.entrySet()) {
                // 如果请求参数中有中文，需要进行URLEncoder编码

                sb.append(entry.getKey())
                        .append("=")
                        .append(entry.getValue());

                sb.append("&");
            }
            sb.deleteCharAt(sb.length() - 1);
            System.out.println(sb.toString());
            Log.d("sb", sb.toString());
        }

        OkGo.<String>delete(sb.toString()).headers(headers)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        callback.onSuccess(response.body(), code);
//                        callback.onSuccess(dataBean.getData().toString());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        callback.onFail(response,code);

                    }
                });
    }
    private void sendGetHeader( Map<String, String> params,String url, final OkgoCallback callback, final int code,HttpHeaders headers) {
        StringBuilder sb = new StringBuilder();
        sb.append(url).append("?");
        if (params != null && params.size() != 0) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                // 如果请求参数中有中文，需要进行URLEncoder编码

                    sb.append(entry.getKey())
                            .append("=")
                            .append(entry.getValue());

                sb.append("&");
            }
            sb.deleteCharAt(sb.length() - 1);
            System.out.println(sb.toString());
            Log.d("sb", sb.toString());
        }

        OkGo.<String>put(sb.toString()).headers(headers)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        callback.onSuccess(response.body(), code);
//                        callback.onSuccess(dataBean.getData().toString());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        callback.onFail(response,code);

                    }
                });
    }



}
